const mongoose = require('./db');
const Schema = mongoose.Schema;


// 张鹏飞
// 创建猜谜语库模型
let riddleSchema = new mongoose.Schema({
    title: String,      // 谜语标题/问题
    answer: String,    // 谜底/答案
    hint: { type: String },                      // 提示信息
    difficulty: { type: Number, default: 1 },    // 难度等级（1-5）
    list: {
        type: [String],  // 字符串数组
        default: []      // 默认空数组
    }     // 答案列表
})

let riddleModel = mongoose.model('riddle', riddleSchema, "riddles")


// 创建社区帖子模型
let postSchema = new mongoose.Schema({
    username: { type: String, required: true },  // 用户名
    avatar: { type: String, default: '/default-avatar.jpg' }, // 头像
    content: { type: String, default: '' },     // 帖子内容
    images: { type: [String], default: [] },    // 图片路径数组
    likes: { type: Number, default: 0 },        // 点赞数
    comments: { type: Number, default: 0 },      // 评论数
    favorites: { type: Number, default: 0 },     // 收藏数
    createdAt: { type: Date, default: Date.now }, // 创建时间
    commentDetails: [{
        username: String,
        avatar: String,
        content: String,
        createdAt: { type: Date, default: Date.now },
        likes: { type: Number, default: 0 },
        location: String,
        replies: [{
            username: String,
            avatar: String,
            content: String,
            createdAt: { type: Date, default: Date.now },
            likes: { type: Number, default: 0 }
        }]
    }]
})

let postModel = mongoose.model('post', postSchema, "posts")

// 刘家兴-商品渲染
let shishouSchema = new mongoose.Schema({
    name: String,
    price: Number,
    sold: Number,
    image: String,
})

let shishouModel = mongoose.model('shishou', shishouSchema, "shishous")


// 刘家兴-评论
let pingjiaSchema = new mongoose.Schema({
    user: String,
    tou: String,
    content: String,
    image: [String],
    zan: { type: Number, default: 0 },
    shoucang: { type: Number, default: 0 },
    createdAt: { type: Date, default: Date.now },
})

let pingjiaModel = mongoose.model('pingjia', pingjiaSchema, "pingjias")



// 王舒畅
let newsSchema = new mongoose.Schema({
    title: String,
    content: String,
    img: String,
    date: { type: Date, default: Date.now },
    source: String,
})

let newsModel = mongoose.model('news', newsSchema, "news")

// 回复模式
const replySchema = new Schema({
    content: { type: String, required: true },
    username: { type: String, required: true },
    avatar: { type: String },
    createdAt: { type: Date, default: Date.now },
    likes: { type: Number, default: 0 }
});

// 评论模式
const commentSchema = new Schema({
    content: { type: String, required: true },
    username: { type: String, required: true },
    avatar: { type: String },
    createdAt: { type: Date, default: Date.now },
    likes: { type: Number, default: 0 },
    replyCount: { type: Number, default: 0 },
    replies: [replySchema]
});

// 非遗知识问答模型
let quizSchema = new mongoose.Schema({
    title: { type: String, required: true },           // 问题标题
    question: { type: String, required: true },        // 问题内容
    options: {
        A: { type: String, required: true },           // 选项A
        B: { type: String, required: true },           // 选项B
        C: { type: String, required: true },           // 选项C
        D: { type: String, required: true }            // 选项D
    },
    correctAnswer: { type: String, required: true },   // 正确答案 (A/B/C/D)
    explanation: { type: String, required: true },     // 答案解析
    category: { type: String, default: '非遗知识' },    // 题目分类
    difficulty: { type: Number, default: 1 },          // 难度等级 1-5
    createdAt: { type: Date, default: Date.now }
});

let quizModel = mongoose.model('quiz', quizSchema, "quizzes");

// 用户答题记录模型
let quizRecordSchema = new mongoose.Schema({
    userId: { type: String, required: true },          // 用户ID
    quizIds: [{ type: mongoose.Schema.Types.ObjectId, ref: 'quiz' }], // 题目ID数组
    userAnswers: [String],                             // 用户答案数组
    correctAnswers: [String],                          // 正确答案数组
    score: { type: Number, required: true },           // 得分
    totalQuestions: { type: Number, required: true },  // 总题数
    timeSpent: { type: Number, required: true },       // 用时(秒)
    completedAt: { type: Date, default: Date.now }
});

let quizRecordModel = mongoose.model('quizRecord', quizRecordSchema, "quizRecords");

// 本草纲目问答模型
let bencaoQuizSchema = new mongoose.Schema({
    title: { type: String, required: true },           // 问题标题
    question: { type: String, required: true },        // 问题内容
    options: {
        A: { type: String, required: true },           // 选项A
        B: { type: String, required: true },           // 选项B
        C: { type: String, required: true },           // 选项C
        D: { type: String, required: true }            // 选项D
    },
    correctAnswer: { type: String, required: true },   // 正确答案 (A/B/C/D)
    explanation: { type: String, required: true },     // 答案解析
    category: { type: String, default: '本草纲目' },    // 题目分类
    difficulty: { type: Number, default: 1 },          // 难度等级 1-5
    createdAt: { type: Date, default: Date.now }
});

let bencaoQuizModel = mongoose.model('bencaoQuiz', bencaoQuizSchema, "bencaoQuizzes");

// 本草纲目用户答题记录模型
let bencaoQuizRecordSchema = new mongoose.Schema({
    userId: { type: String, required: true },          // 用户ID
    quizIds: [{ type: mongoose.Schema.Types.ObjectId, ref: 'bencaoQuiz' }], // 题目ID数组
    userAnswers: [String],                             // 用户答案数组
    correctAnswers: [String],                          // 正确答案数组
    score: { type: Number, required: true },           // 得分
    totalQuestions: { type: Number, required: true },  // 总题数
    timeSpent: { type: Number, required: true },       // 用时(秒)
    completedAt: { type: Date, default: Date.now }
});

let bencaoQuizRecordModel = mongoose.model('bencaoQuizRecord', bencaoQuizRecordSchema, "bencaoQuizRecords");

module.exports = {
    riddleModel,
    shishouModel,
    postModel,
    pingjiaModel,
    newsModel,
    quizModel,
    quizRecordModel,
    bencaoQuizModel,
    bencaoQuizRecordModel
}